package com.example.demo1.controller;

import com.example.demo1.model.User;
import com.example.demo1.model.UserVo;
import com.example.demo1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jyangzi5
 * @version 1.0
 * @description: TODO
 * @date 2021/4/19 11:26
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 新增用户
     * @param user
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Object add(User user) {
        Map<String, Object> result = new HashMap<String, Object>(16);
        userService.saveUser(user);
        result.put("code", 200);
        result.put("msg","保存信息成功");
        return result;
    }

    /**
     * 删除信息，支持批量删除
     *
     * @param ids
     * @return
     */
    @RequestMapping("/del")
    @ResponseBody
    public Object del(String ids) {
        Map<String, Object> result = new HashMap<String, Object>(16);
        userService.deleteUser(ids.split(","));
        result.put("code", 200);
        result.put("msg","删除成功");
        return result;
    }

    /**
     * @param user
     * @return
     */
    @RequestMapping("/modify")
    @ResponseBody
    public Object modify( User user) {
        Map<String, Object> result = new HashMap<String, Object>();
        userService.modifyUser(user);
        result.put("code", 200);
        result.put("msg","更新信息成功");
        return result;
    }

    /**
     * @param userVo
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(UserVo userVo) {
        List<User> userList = userService.getUserList(userVo);
        //int count = userService.getUserCount(userVo);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code", 200);
        //result.put("count", count);
        result.put("data", userList);
        return result;
    }

    /**
     * 编辑
     * @param id
     * @return
     */
    @RequestMapping("/get")
    @ResponseBody
    public Object get(Long id) {
        User user = userService.getUserById(id);
        return user;
    }
}
